home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / dsniff / decode_smb.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  1.7 KB  |  87 lines

  1. /*
  2.   decode_smb.c
  3.  
  4.   Microsoft Server Message Block.
  5.   
  6.   Copyright (c) 2000 Dug Song <dugsong@monkey.org>
  7.  
  8.   $Id: decode_smb.c,v 1.1 2000/05/16 17:31:14 dugsong Exp $
  9. */
  10.  
  11. #include <sys/types.h>
  12. #include <arpa/nameser.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "decode.h"
  16.  
  17. struct smbhdr {
  18.     u_char    proto[4];
  19.     u_char    cmd;
  20.     u_char    err[4];
  21.     u_char    flags1;
  22.     u_short    flags2;
  23.     u_short    pad[6];
  24.     u_short    tid, pid, uid, mid;
  25. };
  26.  
  27. int
  28. decode_smb(u_char *buf, int len)
  29. {
  30.     struct smbhdr *smb;
  31.     int i, j, k;
  32.     u_char *p, *q, *end;
  33.     char *user, *pass;
  34.     
  35.     Buf[0] = '\0';
  36.     
  37.     /* Skip NetBIOS session request. */
  38.     if (len < 4 || buf[0] != 0x81) return (0);
  39.     buf += 2;
  40.     GETSHORT(i, buf); len -= 4;
  41.     if (len < i) return (0);
  42.     buf += i; len -= i;
  43.     end = buf + len;
  44.     
  45.     /* Parse SMBs. */
  46.     for (p = buf; p < end; p += i) {
  47.         GETLONG(i, p);
  48.         if (i > end - p || i < sizeof(*smb) + 32)
  49.             continue;
  50.         
  51.         smb = (struct smbhdr *)p;
  52.         if (memcmp(smb->proto, "\xffSMB", 4) != 0 || smb->cmd != 0x73)
  53.             continue;
  54.         
  55.         user = pass = NULL;
  56.         q = (u_char *)(smb + 1);
  57.         
  58.         if (*q == 10) {        /* Pre NT LM 0.12 */
  59.             q += 15; j = pletohs(q); q += 2;
  60.             if (j > i - (sizeof(*smb) + 15 + 6))
  61.                 continue;
  62.             pass = q + 6;
  63.             user = pass + j;
  64.         }
  65.         else if (*q == 13) {    /* NT LM 0.12 */
  66.             q += 15; j = pletohs(q);
  67.             q += 2;  k = pletohs(q);
  68.             if (j > i - ((q - p) + 12) || k > i - ((q - p) + 11))
  69.                 continue;
  70.             pass = q + 12;
  71.             user = pass + j + k;
  72.         }
  73.         else continue;
  74.         
  75.         /* XXX - skip null IPC sessions, etc. */
  76.         if (user && pass && strlen(user) &&
  77.             is_ascii_string(pass, j - 1)) {
  78.             strlcat(Buf, user, sizeof(Buf));
  79.             strlcat(Buf, " ", sizeof(Buf));
  80.             strlcat(Buf, pass, sizeof(Buf));
  81.             strlcat(Buf, "\n", sizeof(Buf));
  82.         }
  83.     }
  84.     return (strlen(Buf));
  85. }
  86.  
  87.